home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cpptutor.arc / INHERIT6.CPP < prev    next >
Text File  |  1991-04-28  |  3KB  |  136 lines

  1.                                   // Chapter 8 - Program 6
  2. #include "iostream.h"
  3.  
  4. class vehicle {
  5.    int wheels;
  6.    float weight;
  7. public:
  8.    void initialize(int in_wheels, float in_weight);
  9.    int get_wheels(void) {return wheels;}
  10.    float get_weight(void) {return weight;}
  11.    float wheel_loading(void) {return weight/wheels;}
  12. };
  13.  
  14.  
  15.  
  16.  
  17. class car : public vehicle {
  18.    int passenger_load;
  19. public:
  20.    void initialize(int in_wheels, float in_weight, int people = 4);
  21.    int passengers(void) {return passenger_load;}
  22. };
  23.  
  24.  
  25.  
  26.  
  27. class truck : public vehicle {
  28.    int passenger_load;
  29.    float payload;
  30. public:
  31.    void init_truck(int how_many = 2, float max_load = 24000.0);
  32.    float efficiency(void);
  33.    int passengers(void) {return passenger_load;}
  34. };
  35.  
  36.  
  37.  
  38.  
  39. main()
  40. {
  41. vehicle unicycle;
  42.  
  43.    unicycle.initialize(1, 12.5);
  44.    cout << "The unicycle has " << 
  45.                                 unicycle.get_wheels() << " wheel.\n";
  46.    cout << "The unicycle's wheel loading is " << 
  47.          unicycle.wheel_loading() << " pounds on the single tire.\n";
  48.    cout << "The unicycle weighs " << 
  49.                              unicycle.get_weight() << " pounds.\n\n";
  50.  
  51. car sedan[3];
  52. int index;
  53.  
  54.    for (index = 0 ; index < 3 ; index++) {
  55.       sedan[index].initialize(4, 3500.0, 5);
  56.       cout << "The sedan carries " << sedan[index].passengers() << 
  57.                                                     " passengers.\n";
  58.       cout << "The sedan weighs " << sedan[index].get_weight() << 
  59.                                                         " pounds.\n";
  60.       cout << "The sedan's wheel loading is " << 
  61.              sedan[index].wheel_loading() << " pounds per tire.\n\n";
  62.    }
  63.  
  64. truck *semi;
  65.  
  66.    semi = new truck;
  67.    semi->initialize(18, 12500.0);
  68.    semi->init_truck(1, 33675.0);
  69.    cout << "The semi weighs " << semi->get_weight() << " pounds.\n";
  70.    cout << "The semi's efficiency is " << 
  71.                           100.0 * semi->efficiency() << " percent.\n";
  72.    delete semi;
  73. }
  74.  
  75.  
  76.  
  77.  
  78.               // initialize to any data desired
  79. void
  80. vehicle::initialize(int in_wheels, float in_weight)
  81. {
  82.    wheels = in_wheels;
  83.    weight = in_weight;
  84. }
  85.  
  86.  
  87.  
  88. void
  89. car::initialize(int in_wheels, float in_weight, int people)
  90. {
  91.    passenger_load = people;
  92.    vehicle::initialize(in_wheels, in_weight);
  93. }
  94.  
  95.  
  96.  
  97.  
  98. void
  99. truck::init_truck(int how_many, float max_load)
  100. {
  101.    passenger_load = how_many;
  102.    payload = max_load;
  103. }
  104.  
  105.  
  106.  
  107. float
  108. truck::efficiency(void)
  109. {
  110.    return payload / (payload + get_weight());
  111. }
  112.  
  113.  
  114.  
  115.  
  116. // Result of execution
  117. //
  118. // The unicycle has 1 wheel.
  119. // The unicycle's wheel loading is 12.5 pounds on the single tire.
  120. // The unicycle weighs 12.5 pounds.
  121. //
  122. // The sedan carries 5 passengers.
  123. // The sedan weighs 3500 pounds.
  124. // The sedan's wheel loading is 875 pounds per tire.
  125. //
  126. // The sedan carries 5 passengers.
  127. // The sedan weighs 3500 pounds.
  128. // The sedan's wheel loading is 875 pounds per tire.
  129. //
  130. // The sedan carries 5 passengers.
  131. // The sedan weighs 3500 pounds.
  132. // The sedan's wheel loading is 875 pounds per tire.
  133. //
  134. // The semi weighs 12500 pounds.
  135. // The semi's efficiency is 72.929072 percent.
  136.